package com.liqw.crawler.crawler.downloader;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.AbstractDownloader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;
import us.codecraft.webmagic.selector.Selectable;

import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SeleniumDownloader extends AbstractDownloader implements Closeable {

    private WebDriver driver;

    public SeleniumDownloader() {
        // 配置Chrome选项
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.setPageLoadStrategy(org.openqa.selenium.PageLoadStrategy.NORMAL);
        // 1. 基础反检测配置
        options.addArguments("--disable-blink-features=AutomationControlled"); // 核心：禁用自动化控制标记
        options.addArguments("--disable-dev-shm-usage"); // 解决共享内存不足问题
        options.addArguments("--no-sandbox"); // 非沙箱模式（Linux环境常用）
        options.addArguments("--disable-extensions"); // 禁用扩展
        options.addArguments("--disable-gpu"); // 禁用GPU加速
        this.driver = new ChromeDriver();
    }


    @Override
    public Page download(Request request, Task task) {
        try {
            log.info("正在爬取url:{}", request.getUrl());
            // 打开目标页面
            driver.get(request.getUrl());

            // 获取渲染后的页面内容
            String html = driver.getPageSource();

            // 创建Page对象并设置内容
            Page page = Page.ofSuccess(request);
            if (!request.isBinaryContent()) {
                page.setCharset(request.getCharset());
            }
            page.setRawText(html);
            page.setRequest(request);
            page.setUrl(new PlainText(request.getUrl()));
            page.setHtml(new Html(html, request.getUrl()));
            page.setRequest(request);
            page.setBytes(html.getBytes());
            page.setStatusCode (200);
            return page;
        } catch (Exception e) {
            log.error("爬取失败:{}", e.getMessage());
            Page errorPage = new Page();
            errorPage.setRequest(request);
            errorPage.setDownloadSuccess(false);
            return errorPage;
        }
    }

    @Override
    public void setThread(int thread) {
        // 多线程配置（浏览器驱动通常不支持多线程，需特殊处理）
    }

    @Override
    public void close() throws IOException {
        // 关闭浏览器
        if (driver != null) {
            driver.quit();
        }
    }
}
